<%@page import="my.db.DAO.AuthorDAO"%>
<%@page import="my.db.DTO.AuthorDTO"%>
<%@page import="java.util.ArrayList"%>
<%@ include file="sites/ad_header.jsp" %>
<%
	if(session.getAttribute("user")==null){
		response.sendRedirect("LoadIndex.jsp");
	}
%>
<%
		AuthorDAO audao=new AuthorDAO();
		String keyword = request.getParameter("keyword") == null ? "" : request.getParameter("keyword");
		int pages = request.getParameter("pages") == null ? 0 : Integer.parseInt(request.getParameter("pages"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		
		int first = 1;
		int last = (int) Math.ceil((double) audao.countRowsAuthor(keyword) / rows);
	%>
		<div class="r-body">
				<div class="page-title">
					<p>Author Information</p>
				</div>
				<div class="search-panel">
					<input type="text" name="search_article" id="text-search" class="txt-search" placeholder="Search Author..."/>
				</div><!-- end search panel -->				
				<div class="grid-data">
				<%
					ArrayList<AuthorDTO> adto=(ArrayList<AuthorDTO>)request.getAttribute("list_author");
					if(adto!=null){
				%>
				<script type="text/javascript">	
					function checkall() {
						var check=document.getElementById("checkid");
						if(check.checked==true){			
							var check1=document.getElementByClassName("checkvalue");
							for(var i=1;i<check1.length;i++){
								check1[i].checked=true;
							}
							
						}			    		
					}
				</script>
				<form action="./delete_author.article" method="post" name="frm_delete">
					<table border="0" class="tb-data" cellpadding="8">
						<thead>
							<tr>
								<th ><input type="checkbox" onclick="checkall()" class="check-all" name="check_all" id="checkid"/></th>
								<th>Author ID</th>
								<th>Author Name</th>
								<th>Create Date</th>
								<th>Status</th>
								<th>
									<a href="register.jsp"><img src="Image/New.png"/></a>&nbsp;&nbsp;
									<a href="#" onclick="deleteAuthor()"  class="delete"  style="padding:8px;background:#fff;display:inline-block;position:relative;top:-8px;margin-bottom:-10px;">
										<img src="Image/r_delete.png" title="Delete Author"  />
									</a>
								</th>
							</tr>
						</thead>
						
						<tbody>
							<% 
								for(int i=0;i<adto.size();i++){
									AuthorDTO d=adto.get(i);
							%>
							<tr>
							
							<td class="td-center">
									<input  type="checkbox" name="check_id" value="<%=d.getAuthorID()%>" class="check_id"/>																		
							</td>
								<td class="td-center">
									<%=d.getAuthorID() %>
								</td>
								<td>
									<%=d.getAuthorName() %>
								</td>
								<td class="td-center">
									<%=d.getAuthorDate() %>
								</td>			
								<td class="td-center">
									<% if(d.getAuthorStatus()==1){ %>
										<img src="Image/enable.png" title="Detail"/>
									<%}else{ %>
										<img src="Image/disable.png" title="Detail"/>
									<%} %>
								</td>
								<td class="td-center" width="130">							
									<a href="#" onclick="editAuthor(<%=d.getAuthorID() %>)" class="edit"><img src="Image/r_edit.png" title="Edit Author" /></a>&nbsp;&nbsp;
									<a href="./AuthorDetail.article?authordetailid=<%=d.getAuthorID() %>" class="detail"><img src="Image/r_detail.png" title="Author Detail" /></a>
								</td>
							</tr>
							<%	}}
							%>
						</tbody>
					</table>
					 &nbsp;&nbsp;
					</form>
				</div><!-- end datagrind -->
				
				
				<div class="pagination-panel">
						 <div class="pagination">
           			 		<ul>
	           			 		<li>
	           			 			<a class="last_p" href="./AdminPage.article?rows=5&pages=1&keyword=<%=keyword%>&status=<%=s%>"> << </a>
	           			 		</li>
								<li>
									<a class="next_p" href="./AdminPage.article?rows=5&pages=<%=pages - 1 < 2 ? first : pages - 1%>&keyword=<%=keyword%>"> < </a> 
								</a></li>
								
								 <%
						 			String style="style='color:red'";
						            for(int i=1; i<=last; i++){
						            	if (i==pages){
					            %>
					            <li>
					            	<a href="./AdminPage.article?rows=<%=rows%>&pages=<%=i%>&keyword=<%=keyword%>"><%=i%></a>
					            	
					            </li>
					            	
				           		<%
				           			} else {
				           		%>
				           		<li>
				           			<a href="./AdminPage.article?rows=<%=rows%>&pages=<%=i%>&keyword=<%=keyword%>"><%=i%></a>
				           		</li>		
				           		<%
				           			}
					            }%>
								
								<li>
									 <a class="prev" href="./AdminPage.article?rows=5&pages=<%= pages + 1 > last ? last : pages + 1%>&keyword=<%=keyword%>"> > </a>
								</li>
								<li>
									<a class="first" href="./AdminPage.article?rows=5&pages=<%=last%>&keyword=<%=keyword%>"> >> </a>
								</li>
           			 		</ul>    
           			 		
						<div class="go-page">
							<input type="text" class="go-to-page number"/>
							<input type="button" value=" GO "/>
						</div>
           			</div>
					</div>
				</div><!-- end pagination -->
			</div><!-- end body -->
	</div><!-- end r-main -->
	<script language="javascript" type="text/javascript">
			function deleteAuthor() {
			    	var r = confirm("Do you want to delete?");
			    	if (r == true) {
			       	window.location.href="javascript:deleteUsers()";
			    	}  
			} 
							function deleteUsers(){
								frm_delete.submit();
								}
							function editAuthor(id){
								window.location.href="./edit_author.article?author_id="+id+"";
							}  
	</script>
	
	<script type="text/javascript">
		//text-search
		$(document).ready(function(){
			 $('#text-search').keypress(function(e){
			      if(e.keyCode==13){
			    	  var key = document.getElementById("text-search").value;
						window.location.href="./AdminPage.article?rows=5&pages=1&keyword="+ key;
			      }
			      
			   });
		});
		$(function(){
			//alert("HI");
			$(".check-all").change(function(){
				var ch=$(this).prop('checked');
				$(".check_id").prop("checked",ch);
			});
		});
	</script>
</body>
</html>